package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewCartServlet extends HttpServlet {
    private LogService logService;
    private Account account;
    public static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Cart cart = (Cart)session.getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }
        account = (Account)session.getAttribute("account");
        if (account != null){
            logService = new LogService();
            logService.insertLog("View Cart","View Cart", account.getUsername());
        } else {
            logService = new LogService();
            logService.insertLog("View Cart","View Cart", "null");
        }
        request.getRequestDispatcher(VIEW_CART).forward(request,response);
    }
}
